package com.mazaiting.redeye.base.interceptors

import com.mazaiting.log.L
import com.mazaiting.redeye.base.Config
import okhttp3.Interceptor
import okhttp3.Response
import okio.Buffer
import java.nio.charset.Charset

/***
 *
 *
 *                                                    __----~~~~~~~~~~~------___
 *                                   .  .   ~~//====......          __--~ ~~
 *                   -.            \_|//     |||\\  ~~~~~~::::... /~
 *                ___-==_       _-~o~  \/    |||  \\            _/~~-
 *        __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *    _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *  .~       .~       |   \\ -_    /  /-   /   ||      \   /
 * /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 * |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *          '         ~-|      /|    |-~\~~       __--~~
 *                      |-~~-_/ |    |   ~\_   _-~            /\
 *                           /  \     \__   \/~                \__
 *                       _--~ _/ | .-~~____--~-/                  ~~==.
 *                      ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                 -_     ~\      ~~---l__i__i__i--~~_/
 *                                 _-~-__   ~)  \--______________--~~
 *                               //.-~~~-~_--~- |-------~~~~~~~~
 *                                      //.-~~~--\
 *                               神兽保佑
 *                              代码无BUG!
 * @author mazaiting
 * @date 2019-06-27
 * @description 请求拦截器: 统一发送的请求
 */
class RequestInterceptor : Interceptor {
    companion object {
        // 不打印日志的 URL
        val URLS = arrayListOf(Config.URL_FACE_DETECT)
    }

    /**
     * 1. 检测方法是否为 POST
     * 2. 是否需要统一请求
     */
    override fun intercept(chain: Interceptor.Chain): Response {
        // 获取请求
        val request = chain.request()
        // 获取请求方法
        val method = request.method()
        L.d("method: $method")
        val path = request.url().uri().path
        L.d("path: $path")
        var isJump = false
        // 遍历
        for (index in URLS.indices) {
            if (URLS[index] == path) {
                isJump = true
                break
            }
        }
        // 检测是否跳过
        if (!isJump) {
            // 检测是否为 POST 请求
            if ("POST" == method) {
                // 获取请求体
                val requestBody = request.body()
                // 检测是否有请求体
                if (null != requestBody) {
                    // 获取响应类型
                    val contentType = requestBody.contentType()
                    L.d(contentType.toString())
                    // 打印响应体
                    val buffer = Buffer()
                    requestBody.writeTo(buffer)
                    val body = buffer.readString(Charset.defaultCharset())
                    L.d("body: $body")
//                val requestBean = Request("token", Gson().fromJson(body, Any::class.java))
//                L.d("requestBean: $requestBean")
//                val newRequestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), requestBean.toString())
//                val newRequest = request.newBuilder().post(newRequestBody).build()
//                return chain.proceed(newRequest)
                }
            }
        }
        // 获取响应
        return chain.proceed(request)
    }

}